#include <linux/types.h>

#if 0
void enter_syscall(unsigned char flag)
{
    asm volatile (
        "mov r7,%0\n\t"
        "swi 0\n\t"
        :
        :"r"(flag)
        :"r7"
    );
}
#else
void enter_syscall(unsigned char flag)
{
    register long r7 asm("r7") = flag;  // 系统调用号
    register long r0 asm("r0");           // 返回值
    asm volatile (
        "svc #0"  // 触发系统调用
        : "=r"(r0)  // 返回值存储在 r0
        : "r"(r7)   // 系统调用号存储在 r7
        : "memory"
    );
    // if (r0 < 0) {
    //     errno = -r0;  // 设置 errno
    //     r0 = -1;
    // }
    // return r0;
}
#endif

void putc(unsigned char c)
{
    enter_syscall(0);
}

unsigned char getc(void)
{
    enter_syscall(1);
    register unsigned long retval asm ("r0");
    return retval;
}

void exit(void)
{
    enter_syscall(2);
}

void exec(const char *path_name, char **argv)
{
    enter_syscall(3);
}

void show_task(void)
{
    enter_syscall(4);
}

void readdir(void)
{
    enter_syscall(5);
    register unsigned long retval asm ("r0");
    return retval;
}

void waitpid(void)
{
    enter_syscall(6);
}

void kill(int pid)
{
    enter_syscall(7);
}

void fork(void)
{
    enter_syscall(8);
}

int open(void)
{
    enter_syscall(9);
    register unsigned long retval asm ("r0");
    return retval;
}

int read(void)
{
    enter_syscall(10);
    register unsigned long retval asm ("r0");
    return retval;
}

long mmap(void)
{
    enter_syscall(11);
    register unsigned long retval asm ("r0");
    return retval;
}

long getcwd(void)
{
    enter_syscall(12);
    register unsigned long retval asm ("r0");
    return retval;
}

long chdir(void)
{
    enter_syscall(13);
    register unsigned long retval asm ("r0");
    return retval;
}

long mount(void)
{
    enter_syscall(14);
    register unsigned long retval asm ("r0");
    return retval;
}

int write(void)
{
    enter_syscall(15);
    register unsigned long retval asm ("r0");
    return retval;
}

int mkdir(void)
{
    enter_syscall(16);
    register unsigned long retval asm ("r0");
    return retval;
}
